#include "GameChatLayer.h"
#include "Game/GameLib.h"

bool GameChatLayer::init()
{
	if (!Layer::init())
		return false;

// 	LayerColor* layerColor = LayerColor::create(Color4B(0, 0, 0, 180));
// 	addChild(layerColor);
// 	layerColor->setContentSize(Director::getInstance()->getVisibleSize());

	layoutMain = CSLoader::createNode("chatLayer.csb");
	addChild(layoutMain);

	Layout* panelChat = (Layout*)layoutMain->getChildByName("Panel_chat");

	btn_chat = (Button*)panelChat->getChildByName("Button_chatWord");
	btn_chat->addTouchEventListener(CC_CALLBACK_2(GameChatLayer::BtnCallBack, this));
	btn_chat->setBright(false);

	btn_face = (Button*)panelChat->getChildByName("Button_chatFace");
	btn_face->addTouchEventListener(CC_CALLBACK_2(GameChatLayer::BtnCallBack, this));

	auto btn = (Button*)panelChat->getChildByName("Button_send");
	btn->addTouchEventListener(CC_CALLBACK_2(GameChatLayer::BtnCallBack, this));

	list_chat = (ListView*)panelChat->getChildByName("ListView_chat");
	list_chat->addEventListenerListView(this, listvieweventselector(GameChatLayer::selectedItemEvent));

	list_face = (ListView*)panelChat->getChildByName("ListView_face");

	return true;
}

void GameChatLayer::onEnter()
{
	Layer::onEnter();
	auto listener = EventListenerTouchOneByOne::create();
	listener->setSwallowTouches(true);
	listener->onTouchBegan = CC_CALLBACK_2(GameChatLayer::onTouchBegan, this);

	_eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this);
	_touchListener = listener;

	initListView();
	showListView();
}

void GameChatLayer::onExit()
{
	_eventDispatcher->removeEventListener(_touchListener);
	_touchListener = nullptr;
	Layer::onExit();
}

bool GameChatLayer::onTouchBegan( Touch *pTouch, Event *pEvent )
{
	if (!layoutMain->getChildByName("Panel_chat")->getBoundingBox().containsPoint(pTouch->getLocation()))
	{
		this->runAction(RemoveSelf::create());
	}
	return true;
}

void GameChatLayer::showRightPos()
{
	Layout* pChat = (Layout*)layoutMain->getChildByName("Panel_chat");
	Vec2 endPos = pChat->getPosition();
	endPos.x += Director::getInstance()->getWinSize().width - pChat->getContentSize().width;
	pChat->setPosition(endPos);
}

void GameChatLayer::BtnCallBack(Ref* pSender, Widget::TouchEventType touchtype)
{
	Button* btn = (Button*)pSender;
	if (!btn) return;
	switch (touchtype)
	{
	case cocos2d::ui::Widget::TouchEventType::ENDED:
	{
		SoundFun::playEffect("effect_click.mp3");
		if (btn->getName().compare("Button_chatWord") == 0)
		{
			btn_chat->setBright(false);
			btn_face->setBright(true);
			showListView(0);
		}
		else if (btn->getName().compare("Button_chatFace") == 0)
		{
			btn_chat->setBright(true);
			btn_face->setBright(false);
			showListView(1);
		}
		else if (btn->getName().compare("Button_send") == 0)
		{
			Layout* panelChat = (Layout*)layoutMain->getChildByName("Panel_chat");
			TextField* tfield = (TextField*)panelChat->getChildByName("TextField_edit");
			std::string editString = tfield->getString();
			if (!editString.empty())
			{
				CMD_GR_C_UserChat kNetInfo;
				kNetInfo.dwTargetUserID = UserInfo::Instance().getUserID();
				kNetInfo.dwChatColor = 1;
				strcpy(kNetInfo.szChatString, editString.c_str());
				kNetInfo.wChatLength = sizeof(kNetInfo.szChatString);

				CServerItem::get()->SendSocketData(MDM_GR_USER, SUB_GR_USER_CHAT, &kNetInfo, sizeof(kNetInfo));

				this->runAction(RemoveSelf::create());
			}
		}
	}
		break;
	default:
		break;
	}
}

void GameChatLayer::initListView()
{
	Layout* chatItem = (Layout*)layoutMain->getChildByName("Panel_chatItem");
	for (int i = 0; i < MAX_CHAT_ITEM; ++i)
	{
		Layout* newItem = (Layout*)chatItem->clone();
		Text* tword = (Text*)newItem->getChildByName("Text_chat");
		tword->setString(utility::getScriptString(StringUtils::format("chat_default_%d", i)));
		list_chat->pushBackCustomItem(newItem);
	}

	Layout* faceItem = (Layout*)layoutMain->getChildByName("Panel_faceItem");
	for (int i = 0; i < MAX_FACE_V; ++i)
	{
		Layout* newItem = (Layout*)faceItem->clone();
		for (int j = 0; j < MAX_FACE_H; ++j)
		{
			int index = i * MAX_FACE_V + j;
			Button* newBtn = Button::create(StringUtils::format("chat/face%d.png", index + 1), StringUtils::format("chat/face%d.png", index + 1));
			newItem->addChild(newBtn);
			newBtn->setTag(index);
			newBtn->setPosition(Vec2(j * newBtn->getContentSize().width + newBtn->getContentSize().width / 2, newBtn->getContentSize().height / 2));
			newBtn->addClickEventListener([=](Ref* pSender)
			{
				CMD_GR_C_UserExpression kNetInfo;
				kNetInfo.dwTargetUserID = UserInfo::Instance().getUserID();
				kNetInfo.wItemIndex = index;
				CServerItem::get()->SendSocketData(MDM_GR_USER, SUB_GR_USER_EXPRESSION, &kNetInfo, sizeof(kNetInfo));

				this->runAction(RemoveSelf::create());
			});
		}
		list_face->pushBackCustomItem(newItem);
	}
}

void GameChatLayer::showListView(int flag)
{
	list_face->setVisible(false);
	list_chat->setVisible(false);
	if (flag == 0)
		list_chat->setVisible(true);
	else if (flag == 1)
		list_face->setVisible(true);
}

void GameChatLayer::selectedItemEvent(cocos2d::Ref *pSender, ListViewEventType type)
{
	switch (type)
	{
		case cocos2d::ui::LISTVIEW_ONSELECTEDITEM_END:
		{
			ListView* listView = static_cast<ListView*>(pSender);
			int index = listView->getCurSelectedIndex();
			//log("%ld", listView->getCurSelectedIndex());

			CMD_GR_C_UserExpression kNetInfo;
			kNetInfo.dwTargetUserID = UserInfo::Instance().getUserID();
			kNetInfo.wItemIndex = index + 1000;
			CServerItem::get()->SendSocketData(MDM_GR_USER, SUB_GR_USER_EXPRESSION, &kNetInfo, sizeof(kNetInfo));
			this->runAction(RemoveSelf::create());
			/*
			Layout* panelChat = (Layout*)layoutMain->getChildByName("Panel_chat");
			TextField* tfield = (TextField*)panelChat->getChildByName("TextField_edit");
			tfield->setString(utility::getScriptString(StringUtils::format("chat_default_%d", index)));
			*/
		}
		break;
	default:
		break;
	}
}
